import {
  find
} from '../../../api/order';
import {
  save
} from "../../../api/review"
const util = require('../../../utils/util.js')
Page({
  data: {
    logistics: [
      false, false, false, false, false
    ],
    logisticsIndex: 0,
    logisticsTitle: '非常差',
    textIndex: 0,
    icon: [false, false, false, false, false],
    evaluationData: []
  },
  onLoad: function(options) {
    var _this = this;
    console.log(options)
    _this.data.id = options.id
    _this.findClick(options.id)
  },

  findClick(data) {
    var _this = this
    console.log(data)
    find(data).then(
      res => {
        console.log(res)
        if (res.type == 'success') {
          res.data.order.itemList.forEach(function(item) {
            var itemEvaluation = {
              content: '',
              itemList: [],
              name: item.name,
              orderId: item.orderId,
              price: item.price,
              productId: item.productId,
              quantity: item.quantity,
              spec: item.spec,
              star: 4,
              thumbnail: item.thumbnail,
              unit: item.unit
            }
            _this.data.evaluationData.push(itemEvaluation)
          })
          _this.setData({
            evaluationData: _this.data.evaluationData
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },

  logistics(data) {
    console.log(data)
    this.data.logisticsIndex = data.target.dataset.index;
    this.data.logisticsTitle = this.stars(data.target.dataset.index)
    this.setData({
      logisticsIndex: this.data.logisticsIndex,
      logisticsTitle: this.data.logisticsTitle
    })
  },
  productClick(data) {
    console.log(data)
    var index = data.target.dataset.index;
    var indexs = data.target.dataset.indexs;
    this.data.evaluationData[index].star = indexs
    // this.data.product[index].iconIndex = indexs;
    // this.data.product[index].iconTitle = this.stars(indexs)
    // console.log(this.data.product)
    this.setData({
      evaluationData: this.data.evaluationData
    })
  },
  stars(data) {
    var starsTitle = '';
    if (data == 0) {
      starsTitle = '非常差'
    } else if (data == 1) {
      starsTitle = '差'
    } else if (data == 2) {
      starsTitle = '一般'
    } else if (data == 3) {
      starsTitle = '好'
    } else if (data == 4) {
      starsTitle = '非常好'
    }
    return starsTitle
  },
  textareaIndex(data) {
    console.log(data)
    this.data.textIndex = data.target.dataset.index
  },
  textareaClick(data) {
    console.log(data)
    this.data.evaluationData[this.data.textIndex].content = data.detail.value,
      this.setData({
        evaluationData: this.data.evaluationData
      })
  },
  metaClick(data) {
    console.log(data)
    var _this = this
    _this.data.evaluationData[data.currentTarget.dataset.index].itemList = []
    if (data.detail.images != []) {
      data.detail.images.forEach(function(item) {
        var itemList = {
          type: 1,
          url: item
        }
        _this.data.evaluationData[data.currentTarget.dataset.index].itemList.push(itemList)
      })
    }
    if (data.detail.video != []) {
      data.detail.video.forEach(function(item) {
        var itemList = {
          type: 2,
          url: item
        }
        _this.data.evaluationData[data.currentTarget.dataset.index].itemList.push(itemList)
      })
    }
  },
  confirmClick() {
    var _this = this
    console.log(this.data.evaluationData)
    save(_this.data.evaluationData).then(res => {
      if (res.type == 'success') {
        wx.redirectTo({
            url: '/pages/member/order/evaluation_confirm'
          })
      }
    }).catch(error => {
      console.log(error)
    })
  }
})